package web.demo.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import web.demo.dao.IFoodDetailDao;
import web.demo.entity.FoodDetail;
import web.demo.entity.FoodType;
import web.demo.factory.BeanFactory;
import web.demo.utils.Condition;
import web.demo.utils.JdbcUtils;
import web.demo.utils.PageBean;

public class FoodDetailDao extends BaseDao implements IFoodDetailDao{
	//查询所有的菜品详情
	@Override
	public List<FoodDetail> getAll() {
		StringBuilder sb=new StringBuilder();
		sb.append("select ");
		sb.append("		f.id,");
		sb.append("		f.name,");
		sb.append("		f.price,");
		sb.append("		f.vip_price,");
		sb.append("		f.foodType_id,");
		sb.append("		ft.name AS foodType_name,");
		sb.append("		f.description,");
		sb.append("		f.img ");
		sb.append("from ");
		sb.append("		food f inner join ");
		sb.append("		foodType ft ");
		sb.append("where 1=1 ");
		sb.append("		and f.foodType_id=ft.id ");
		sb.append("		order by f.id asc");
		return select(sb.toString(),FoodDetail.class);
	}
	//查询菜品详情总数，也就是菜品的总数
	@Override
	public int getCount() {
		StringBuilder sb=new StringBuilder();
		sb.append("select ");
		sb.append("		count(f.id) ");
		sb.append("from ");
		sb.append("		food f inner join ");
		sb.append("		foodType ft ");
		sb.append("where 1=1 ");
		sb.append("		and f.foodType_id=ft.id ");
		
		Long count;
		try {
			count = JdbcUtils.getQueryRunner().query(sb.toString(),new ScalarHandler<Long>());
			return count.intValue();
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	//按照分页多条件查询菜品详情，查询当前条件下的菜品详情，可以设定是否分页
	@Override
	public List<FoodDetail> getAll(PageBean pageBean) {
		StringBuilder sb=new StringBuilder();
		sb.append(" select ");
		sb.append("  f.id,");
		sb.append("  f.name,");
		sb.append("	 f.price,");
		sb.append("	 f.vip_price,");
		sb.append("	 f.foodType_id,");
		sb.append("	 ft.name as foodType_name,");
		sb.append("	 f.description,");
		sb.append("	 f.img ");
		sb.append(" from ");
		sb.append("	 food f inner join ");
		sb.append("	 foodType ft ");
		sb.append(" where 1=1 ");
		sb.append("	 and f.foodType_id=ft.id ");
		//获取分页条件
		Condition condition=pageBean.getCondition();
		int foodTypeId=condition.getFoodTypeId();
		String foodName=condition.getFoodName();
		boolean isPage=condition.isPage();
		List<Object> conditionList=new ArrayList<Object>();
		if(foodTypeId>0){
			sb.append("	and f.foodType_id=? ");
			conditionList.add(foodTypeId);
		}
		if(foodName!=null){
			sb.append("	and f.name like ? ");
			conditionList.add("%"+foodName+"%");
		}
		sb.append(" order by f.id asc ");
		if(isPage){
			sb.append(" limit ?,?; ");
			int start=(pageBean.getCurrentPage()-1)*pageBean.getRows();
			int end=pageBean.getRows();
			conditionList.add(start);
			conditionList.add(end);
		}
		return query(sb.toString(),conditionList.toArray(),FoodDetail.class);
	}
	//按照分页多条件查询菜品详情总数，返回的是当前条件下的菜品数目，不管是否分页
	@Override
	public int getCount(PageBean pageBean) {
		StringBuilder sb=new StringBuilder();
		sb.append("select ");
		sb.append("		count(f.id) ");
		sb.append("from ");
		sb.append("		food f inner join ");
		sb.append("		foodType ft ");
		sb.append("where 1=1 ");
		sb.append("		and f.foodType_id=ft.id ");
		//获取分页条件
		Condition condition=pageBean.getCondition();
		int foodTypeId=condition.getFoodTypeId();
		String foodName=condition.getFoodName();
		List<Object> conditionList=new ArrayList<Object>();
		if(foodTypeId>0){
			sb.append("		and f.foodType_id=? ");
			conditionList.add(foodTypeId);
		}
		if(foodName!=null){
			sb.append("		and f.name=? ");
			conditionList.add("%"+foodName+"%");
		}
		try {
			Long count=JdbcUtils.getQueryRunner().query(
					sb.toString(),
					new ScalarHandler<Long>(),
					conditionList.toArray()
					);
			return count.intValue();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	//按照条件查询菜品详情
	@Override
	public List<FoodDetail> search(String selectCondition) {
		StringBuilder sb=new StringBuilder();
		sb.append("select ");
		sb.append("		f.id,");
		sb.append("		f.name,");
		sb.append("		f.price,");
		sb.append("		f.vip_price,");
		sb.append("		f.foodType_id,");
		sb.append("		ft.name AS foodType_name,");
		sb.append("		f.description,");
		sb.append("		f.img ");
		sb.append("from ");
		sb.append("		food f inner join ");
		sb.append("		foodType ft ");
		sb.append("where 1=1 ");
		sb.append("		and f.foodType_id=ft.id ");
		//插入查询条件
		Object params=null;
		if(selectCondition.matches("[0-9]+")){
			//按照食品编号查询
			sb.append(" and f.id=? ");
			params=Integer.parseInt(selectCondition);
		}
		if(selectCondition.matches("[\u4e00-\u9fa5a-zA-Z]+|\\s*")){
			//按照名称查询
			//判断名称是否是菜系名称，如果是菜系名称，则返回该菜系下所有菜品
			FoodTypeDao foodTypeDao=BeanFactory.getInstance("foodTypeDao",FoodTypeDao.class);
			List<FoodType> foodTypeList=foodTypeDao.getAll();
			boolean flag=false;
			for(FoodType foodType:foodTypeList){
				if(foodType.getName().equals(selectCondition)){
					flag=true;
				}
			}
			if(flag){
				sb.append("  and ft.name like ? ");
			}else{
				sb.append("  and f.name like ? ");
			}
			params="%"+selectCondition+"%";
		}
		sb.append("		order by f.id asc");
		
		try{
			return JdbcUtils.getQueryRunner().query(sb.toString(),new BeanListHandler<FoodDetail>(FoodDetail.class),params);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
}
